{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exporting collection to Cloud Storage\n",
    "\n",
    "### toCloudStorage(collection, bucket=\"opencrops\", folder=None, namePattern='{id}', scale=30, dataType=\"float\", region=None, datePattern=None):\n",
    "Export an image clipped by features (Polygons). You can use the same arguments as the original function ee.batch.export.image.toDrive\n",
    "\n",
    "**Parameters**\n",
    "\n",
    "- collection: image collection\n",
    "- bucket: GCS bucket\n",
    "- folder: same as ee.Export\n",
    "- name: name of the resulting image. If `None` uses image's ID\n",
    "- scale: same as ee.Export. Default to 1000\n",
    "- dataType: as downloaded images **must** have the same data type in all\n",
    "    bands, you have to set it here. Can be one of: \"float\", \"double\", \"int\",\n",
    "    \"Uint8\", \"Int8\" or a casting function like *ee.Image.toFloat*\n",
    "- region: geometry to clip export\n",
    "- kwargs: keyword arguments that will be passed to ee.batch.export.image.toCloudStorage\n",
    "\n",
    "Return a list of all tasks (for further processing/checking)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import sys\n",
    "sys.path.append(\"../..\")\n",
    "ee.Initialize()\n",
    "from geetools import batch"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get an Image Collection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "p1 = ee.Geometry.Point([-71,-42])\n",
    "feat1 = ee.Feature(p1.buffer(1000), {'site': 1})\n",
    "fc = ee.FeatureCollection([feat1])\n",
    "collection = ee.ImageCollection('COPERNICUS/S2').filterBounds(fc.geometry()).filterDate(\"2015-01-01\",\"2015-12-01\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "adding task 20150825T143316_20150825T144048_T19GCP to list\n",
      "adding task 20150825T143316_20161001T010844_T19GCP to list\n",
      "adding task 20150825T144048_20160417T022845_T19GCP to list\n",
      "adding task 20151123T142942_20170221T180430_T19GCP to list\n"
     ]
    }
   ],
   "source": [
    "# please ensure that you have a default project enabled\n",
    "# and change this bucket with a bucket you have write access\n",
    "BUCKET = \"opencrops\"\n",
    "\n",
    "batch.imagecollection.toCloudStorage(collection, bucket=BUCKET, folder=\"geetools\", scale=30, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gs://opencrops/geetools/20150825T143316_20150825T144048_T19GCP.tif\r\n",
      "gs://opencrops/geetools/20150825T143316_20161001T010844_T19GCP.tif\r\n",
      "gs://opencrops/geetools/20150825T144048_20160417T022845_T19GCP.tif\r\n",
      "gs://opencrops/geetools/20151123T142942_20170221T180430_T19GCP.tif\r\n"
     ]
    }
   ],
   "source": [
    "# check the storage bucket via the gsutil tool\n",
    "!gsutil ls gs://opencrops/geetools"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
